package com.example.springboot.service.impl;

import com.example.springboot.common.exception.ServiceException;
import com.example.springboot.common.request.CategoryListRequest;
import com.example.springboot.entity.Category;
import com.example.springboot.mapper.BookMapper;
import com.example.springboot.mapper.CategoryMapper;
import com.example.springboot.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.example.springboot.common.exception.ExceptionConstant.Exists_BooksInCategory_ERROR;
import static com.example.springboot.common.exception.ExceptionConstant.Exists_CategoryName_ERROR;


@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private BookMapper bookMapper;


    /**
     * 分页模糊查询
     * @param categoryListRequest
     * @return
     */
    public PageInfo<Category> page(CategoryListRequest categoryListRequest) {
        PageHelper.startPage(categoryListRequest.getPageNum(), categoryListRequest.getPageSize());
        List<Category> categories = categoryMapper.listByName(categoryListRequest);
        return new PageInfo<>(categories);
    }

    /**
     * 添加图书分类
     * @param category
     * @throws Exception
     */
    public void save(Category category) throws Exception {
        // 判断分类名称是否已经存在
        if(categoryMapper.findByName(category.getName()) != null) {
            throw new ServiceException(Exists_CategoryName_ERROR);
        }

        category.setUpdateTime(new Date());
        categoryMapper.save(category);
    }

    /**
     * 修改图书分类信息
     * @param category
     * @throws Exception
     */
    public void update(Category category) throws Exception {
        // 判断分类名称是否已经存在
        if (categoryMapper.findByNameAndId(category) != null) {
            throw new ServiceException(Exists_CategoryName_ERROR);
        }

        // 判断该分类下是否存在书籍
        Category primaryCategory = categoryMapper.getById(category.getId());
        Integer count = bookMapper.selectBookByCategory(primaryCategory.getName());
        if( count != null && count > 0 ) {
            throw new ServiceException(Exists_BooksInCategory_ERROR);
        }

        category.setUpdateTime(new Date());
        categoryMapper.update(category);

    }

    /**
     * 根据id获取需要修改的图书分类信息
     * @param id
     * @return
     */
    public Category getById(Integer id) {
        Category category = categoryMapper.getById(id);
        return category;
    }

    /**
     * 根据id删除分类名称
     * @param id
     */
    public void deleteById(Integer id) {
        // 判断该分类下是否存在书籍
        Category category = categoryMapper.getById(id);
        Integer count = bookMapper.selectBookByCategory(category.getName());
        if( count != null && count > 0 ) {
            throw new ServiceException(Exists_BooksInCategory_ERROR);
        }

        categoryMapper.deleteById(id);
    }

    /**
     * 查询所有图书分类信息
     * @return
     */
    public List<Category> list() {
        return categoryMapper.list();
    }

}
